package cn.smilefox.service.impl;

import cn.smilefox.dao.CourseDao;
import cn.smilefox.dao.RoleDao;
import cn.smilefox.dao.TeacherDao;
import cn.smilefox.domain.Course;
import cn.smilefox.domain.Role;
import cn.smilefox.domain.Teacher;
import cn.smilefox.service.TeacherService;

import java.util.List;

public class TeacherServiceImpl implements TeacherService {

    private TeacherDao teacherDao;
    public void setTeacherDao(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    private RoleDao roleDao;

    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    private CourseDao courseDao;

    public void setCourseDao(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    @Override
    public List<Teacher> list() {
        List<Teacher> teacherList = teacherDao.findall();

        //取出封装的每个teacherlist里面的teacher的id获取其role属性。
        for (Teacher teacher : teacherList) {
            //获得teacher的id
            Long id = teacher.getId();
            //id作为参数查询对于的role集合数据
            List<Role> roles = roleDao.findRoleByUserId(id);
            teacher.setRoles(roles);

        }

        return teacherList;
    }

    @Override
    public void save(Teacher teacher, Long[] roleIds) {
        //第一向teacher里面添加数据
        Long id = teacherDao.save(teacher);
        //第二将teacher对应的关系添加到关联表里面去。
        teacherDao.saveTeacherRoleRel(id,roleIds);

    }

    @Override
    public void del(Long teacherId) {
        //1、删除关系表
        teacherDao.delTeacherRoleRel(teacherId);
        //2、删除teacher表
        teacherDao.del(teacherId);
    }

    @Override
    public List<Teacher> find(Teacher teacher) {
        List<Teacher> teacherList = teacherDao.find(teacher);
        //取出封装的每个teacherlist里面的teacher的id获取其role属性。
        for (Teacher teacher_s : teacherList) {
            //获得teacher的id
            Long id = teacher_s.getId();
            //id作为参数查询对于的role集合数据
            List<Role> roles = roleDao.findRoleByUserId(id);
            teacher_s.setRoles(roles);

        }
        return teacherList;
    }

    //将所有课程查询出来并且统计
    @Override
    public List<Teacher> findallnum() {

        //查询各个教室实验课时
        //1、将所有老师进行查询封装。
        List<Teacher> teacherList = teacherDao.findall();
        //2.循环进行读取id并且通过关联表进行查询相关课程
        for (Teacher teacher : teacherList) {
            Long id = teacher.getId();
            Long test_num = 0L;
            Long theory_num = 0L;
            //3、循环将该教师所有有关课程的实验课时提取出来
            //--------------实验课程课时添加------------------//
            List<Course> courseList = courseDao.findTestById(id);
            for (Course course : courseList) {
                String course_num = course.getCourse_num();
                Long t = Long.valueOf(course_num);
                test_num += t;
            }

            teacher.setTest_num(test_num);
            //---------理论课程课时添加---------------//
            List<Course> courseList2 = courseDao.findTheoryById(id);
            for (Course course : courseList2) {
                String course_num = course.getCourse_num();
                Long t = Long.valueOf(course_num);
                theory_num += t;
            }
            //4、将所有课时进行相加封装进每个course的test_num.
            teacher.setTheory_num(theory_num);
        }
        return teacherList;
    }

    @Override
    public Teacher findCourseNum(Teacher teacher) {
        Teacher teacher2 = new Teacher();
        List<Teacher> teacherList = findallnum();
        for (Teacher teacher1 : teacherList) {
            if (teacher1.getTc_name().equals(teacher.getTc_name())){
                teacher2 = teacher1;
            }
        }

        System.out.println("---------------");
        System.out.println(teacher2);
        return teacher2;
    }

    ;

    //查询特定老师课时信息


}
